//
// Copyright (C) 2024 Kumo group inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <kthread/internal/config.h>

TURBO_FLAG(bool, show_kthread_creation_in_vars, false, "When this flags is on, The time "
           "from kthread creation to first run will be recorded and shown "
           "in /builtin/vars").on_validate(
    turbo::AllPassValidator<bool>::validate);

TURBO_FLAG(bool, show_per_worker_usage_in_vars, false,
           "Show per-worker usage in /builtin/vars/kthread_per_worker_usage_<tid>").on_validate(
    turbo::AllPassValidator<bool>::validate);

TURBO_FLAG(int32_t, stack_size_small, 32768, "size of small stacks");
TURBO_FLAG(int32_t, stack_size_normal, 1048576, "size of normal stacks");
TURBO_FLAG(int32_t, stack_size_large, 8388608, "size of large stacks");
TURBO_FLAG(int32_t, guard_page_size, 4096, "size of guard page, allocate stacks by malloc if it's 0(not recommended)");
TURBO_FLAG(int32_t, tc_stack_small, 32, "maximum small stacks cached by each thread");
TURBO_FLAG(int32_t, tc_stack_normal, 8, "maximum normal stacks cached by each thread");

TURBO_FLAG(int32_t, task_group_delete_delay, 1,
           "delay deletion of TaskGroup for so many seconds");
TURBO_FLAG(int32_t, task_group_runqueue_capacity, 4096,
           "capacity of runqueue in each TaskGroup");
TURBO_FLAG(int32_t, task_group_yield_before_idle, 0,
           "TaskGroup yields so many times before idle");
TURBO_FLAG(int32_t, task_group_ntags, 1, "TaskGroup will be grouped by number ntags");

namespace kthread {
    void setup_kthread_options(turbo::cli::App *app) {
        auto g = app->add_option_group("kthread", "kumo kthread runtime options");

        g->enable_flags_option(FLAGS_show_kthread_creation_in_vars);

        g->enable_flags_option(FLAGS_show_per_worker_usage_in_vars);


        g->enable_flags_option(FLAGS_kthread_concurrency);

        g->enable_flags_option(FLAGS_kthread_min_concurrency);

        g->enable_flags_option(FLAGS_kthread_current_tag);

        g->enable_flags_option(FLAGS_kthread_concurrency_by_tag);

        g->enable_flags_option(FLAGS_stack_size_small);
        g->enable_flags_option(FLAGS_stack_size_normal);
        g->enable_flags_option(FLAGS_stack_size_large);
        g->enable_flags_option(FLAGS_guard_page_size);
        g->enable_flags_option(FLAGS_tc_stack_small);
        g->enable_flags_option(FLAGS_tc_stack_normal);

        g->enable_flags_option(FLAGS_task_group_delete_delay);
        g->enable_flags_option(FLAGS_task_group_runqueue_capacity);
        g->enable_flags_option(FLAGS_task_group_yield_before_idle);
        g->enable_flags_option(FLAGS_task_group_ntags);
    }
} // namespace kthread
